<?php

namespace App\Http\Controllers\Admin\Area;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class CityController extends BaseController {

    /**
     * @api {get} /area/city/get-list-by-province-id 市级行政单位数据-根据省份id获取下辖市列表
     * @apiVersion 0.1.0
     * @apiName 区域信息-市级行政单位数据-根据省份id获取下辖市列表
     * @apiGroup 区域信息

     * @apiParam {string} province_id                 省份id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
        "code": 200,
        "data": [
            {
                "city_id(城市id)": 110000,
                "name(名称)": "北京"
            }
        ]
    }
     *
     *
     */
    public function getListByProvinceId(Request $request) {
        try {
            $this->validate($request, [
                'province_id' => 'required|numeric',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $provinceId = $request->input("province_id");

        $all = app('repo_area')->getAllCityList(["parent_id" => $provinceId]);


        $result = [];
        foreach ($all as $item) {
            $result[] = [
                "city_id" => $item['district_id'],
                "name" => $item['name'],
            ];
        }

        return Response::sendData($result);
    }

}
